package com.aat.restful.springresteasy.app;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;

import org.springframework.stereotype.Component;

@Component
@Provider
public class ServerContainerRequestFilter implements ContainerRequestFilter {

	@Override
	public void filter(ContainerRequestContext requestContext) throws IOException {
		InputStream ins = requestContext.getEntityStream();
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] bs = new byte[1024];
		int i = 0;
		while ((i = ins.read(bs)) > -1) {
			bos.write(bs, 0, i);
		}
		byte[] body = bos.toByteArray();
		bos.close();
		ins.close();
		// chk code
		System.out.println("1");
		UriInfo u = requestContext.getUriInfo();
		{
			System.out.println(u.getPath());
			System.out.println(u.getBaseUri());
			System.out.println(u.getAbsolutePath());
			System.out.println(u.getRequestUri());
		}
		requestContext.abortWith(Response.status(111).entity("").build());
		System.out.println("2");
		// end
		requestContext.setEntityStream(new ByteArrayInputStream(body));
	}

}
